<?php 
require_once("sls_lib/sls_class.inc");

$act = isset( $_REQUEST["act"] ) ? $_REQUEST["act"] : "";
$data = isset( $_REQUEST["data"] ) ? $_REQUEST["data"] : array();
$sls =& new Sls();

switch($act) {
	case "change":
		$url = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
        $locale = (isset($_REQUEST['locale']) && $_REQUEST['locale']!='') ? $_REQUEST['locale'] : $sls->default;
        $sls->setCurrentLang($locale);
        header("Location: ".$url);
		break;
	case "translate":
		if ( isset($data['Locale']) ) {
			$result = checkTransData($data['Locale']);
			if ( $result['valid'] ) {
				$result['added'] = $sls->localeText($data['Locale']['textId'], $data['Locale']['LocaleTranslation']);
			}
			echo json($result);
			die();
		}
	default:
		break;
}

function checkTransData($data) {
	global $sls;
	
	$result = array('valid' => true);
	if ( !isset($data['Password']) || empty($data['Password'])) {
		$result['messages']['Password'] = 'Password cannot be blank';	
		$result['valid'] = false;
	} elseif ( $data['Password'] != $sls->sls_password ) {
		$result['messages']['Password'] = 'Incorrect password';	
		$result['valid'] = false;
	}
	if ( !isset($data['LocaleTranslation']) || empty($data['LocaleTranslation']) ) {
		$result['messages']['LocaleTranslation'] = 'Translation cannot be blank';
		$result['valid'] = false;
	}
	if ( !isset($data['textId']) || empty($data['textId']) ) {
		$result['messages']['textId'] = 'Id cannot be blank';
		$result['valid'] = false;
	}
	return $result;
}

function json($data, $q="\"") {
	static $js = null;
	if ($js === null) {$js = new JsHelper(); }
	return $js->json($data, $q);
}

?>